package tech.wetech.admin.modules.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.AliPayInfoMapper;
import tech.wetech.admin.modules.system.mapper.CompanyMapper;
import tech.wetech.admin.modules.system.mapper.PayCompanyInfoMapper;
import tech.wetech.admin.modules.system.mapper.WXPayInfoMapper;
import tech.wetech.admin.modules.system.po.*;
import tech.wetech.admin.modules.system.service.CompanyService;
import tech.wetech.admin.modules.system.service.PasswordHelper;
import tech.wetech.admin.modules.system.vo.*;
import tk.mybatis.mapper.code.Style;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lu
 * @description 商户管理实现类
 * @date 2019年06月27日 14:12
 */
@Service
@Transactional
public class CompanyServiceImpl extends BaseService<Company> implements CompanyService {

    @Value("${platform}")
    private String platform = "";

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private PasswordHelper passwordHelper;
    @Autowired
    private PayCompanyInfoMapper payCompanyInfoMapper;
    @Autowired
    private WXPayInfoMapper wxPayInfoMapper;

    @Autowired
    private AliPayInfoMapper aliPayInfoMapper;

    /*
    ** @author lu
    * @date 2019/7/3 10:09
    * @param [company]
    * @return void
    * @description 新增
    */
    @Override
    public void createCompany(Company company) {
        // 加密密码
        passwordHelper.companyPassword(company);
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            company.setCreateTime(dd.parse(dd.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取今天的时间对比时间今天生成的单号，获取单号的后三位
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String date=df.format(new Date());
        String strMAXcompanyId= companyMapper.selectMAXcompanyId(date);
        if (StringUtils.isEmpty(strMAXcompanyId)){
            company.setCompanyId(date+"001");
        }else {
            Integer intMAXcompanyId =Integer.valueOf(strMAXcompanyId);
            if (intMAXcompanyId<9){
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+"00"+intMAXcompanyId);
            }else if (9<=intMAXcompanyId && intMAXcompanyId < 99){
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+"0"+intMAXcompanyId);
            }else {
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+intMAXcompanyId);
            }
        }
        company.setCompanyId(company.getCompanyId() + platform);
        companyMapper.insertSelective(company);

        //是否需要配置设备的支付json
        if (!company.getIsEverySet()){
            byte payChannel;
            for (PayCompanyInfo payCompanyInfo : company.getPayCompanyInfos()) {
                ObjectMapper mapper = new ObjectMapper();
                String json="";
                if (payCompanyInfo.getWxPayJsonVo()!=null){
                    payChannel=1;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getWxPayJsonVo());
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(payCompanyInfo.getWxPayJsonVo().getAppID());
                    wxPayInfo.setMchId(payCompanyInfo.getWxPayJsonVo().getMchid());
                    wxPayInfo.setWXkey(payCompanyInfo.getWxPayJsonVo().getKey());
                    wxPayInfo.setCreateTime(d);
                    wxPayInfoMapper.insert(wxPayInfo);
                }else if (payCompanyInfo.getZfbPayJsonVo()!=null){
                    payChannel=2;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getZfbPayJsonVo());
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(payCompanyInfo.getZfbPayJsonVo().getAppID());
                    aliPayInfo.setAliPayPublicKey(payCompanyInfo.getZfbPayJsonVo().getAlipayPublicKey());
                    aliPayInfo.setCreateTime(d);
                    aliPayInfoMapper.insert(aliPayInfo);
                }else if (payCompanyInfo.getIcbcVo()!=null){
                    payChannel=3;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getIcbcVo());
                }else if (payCompanyInfo.getUnionJsonPayVo()!=null){
                    payChannel=4;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getUnionJsonPayVo());
                }else if (payCompanyInfo.getQingDaoNongShangJsonVo()!=null){
                    payChannel=5;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getQingDaoNongShangJsonVo());
                }else if (payCompanyInfo.getShouQianBaVo()!=null){
                    payChannel=6;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getShouQianBaVo());
                }else if (payCompanyInfo.getCcbbVo()!=null){
                    payChannel=7;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getCcbbVo());
                }else if (payCompanyInfo.getKancloudVo()!=null){
                    payChannel=8;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getKancloudVo());
                }else if (payCompanyInfo.getLeShua()!=null){
                    payChannel=9;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getLeShua());
                }else if (payCompanyInfo.getChinaums()!=null){
                    payChannel=10;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getChinaums());
                }else if (payCompanyInfo.getUnionSchool()!=null){
                    payChannel=11;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getUnionSchool());
                }else if (payCompanyInfo.getFuHuiBaVo()!=null){
                    payChannel=12;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getFuHuiBaVo());
                }else if (payCompanyInfo.getKsrcbVo()!=null){
                    payChannel=13;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getKsrcbVo());
                }else if (payCompanyInfo.getSuiXingPay()!=null){
                    payChannel=14;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getSuiXingPay());
                }else if (payCompanyInfo.getAbc()!=null){
                    payChannel=15;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getAbc());
                }else if (payCompanyInfo.getQsf()!=null){
                    payChannel=16;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getQsf());
                }else if (payCompanyInfo.getShengCan()!=null){
                    payChannel=17;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getShengCan());
                }else if (payCompanyInfo.getPosTong()!=null){
                    payChannel=18;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getPosTong());
                }else if (payCompanyInfo.getHuiYinTong()!=null){
                    payChannel=19;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getHuiYinTong());
                } else if (payCompanyInfo.getBocom()!=null){
                    payChannel=20;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getBocom());
                }else if (payCompanyInfo.getSandHippoVo()!=null){
                    payChannel=21;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getSandHippoVo());
                }else if (payCompanyInfo.getLaKaLaVo()!=null){
                    payChannel=22;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getLaKaLaVo());
                }else if (payCompanyInfo.getPsbc()!=null){
                    payChannel=23;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getPsbc());
                }else if (payCompanyInfo.getSaoBei()!=null){
                    payChannel=24;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getSaoBei());
                }else if (payCompanyInfo.getHaiKeVo()!=null){
                    payChannel=25;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getHaiKeVo());
                }else if (payCompanyInfo.getFuYouVo()!=null){
                    payChannel=26;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getFuYouVo());
                }else if (payCompanyInfo.getLongZhiMengVo()!=null){
                    payChannel=27;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getLongZhiMengVo());
                }else if (payCompanyInfo.getAliSchoolVo()!=null){
                    payChannel=28;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getAliSchoolVo());
                }else if (payCompanyInfo.getSuiXingPay()!=null){
                    payChannel=14;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getSuiXingPay());
                }else if (payCompanyInfo.getZhongShanTongVO()!=null){
                    payChannel=29;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getZhongShanTongVO());
                }else if (payCompanyInfo.getZhaoHangYiWangTongVO()!=null){
                    payChannel=30;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getZhaoHangYiWangTongVO());
                }else if (payCompanyInfo.getQuZhouYiMaFuVO()!=null){
                    payChannel=31;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getQuZhouYiMaFuVO());
                }else if (payCompanyInfo.getJianHangShengHuoVO()!=null){
                    payChannel=32;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getJianHangShengHuoVO());
                }else if(payCompanyInfo.getCcbPayVo() != null){
                    payChannel=33;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getCcbPayVo());
                 }else if(payCompanyInfo.getYinshengVo() != null){
                    payChannel=34;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getYinshengVo());
                }else if(payCompanyInfo.getLanzhouVo() != null){
                    payChannel=37;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getLanzhouVo());
                }else if(payCompanyInfo.getNonghangjfVo() != null){
                    payChannel=38;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getNonghangjfVo());
                }

                payCompanyInfo.setCompanyId(company.getCompanyId());

                payCompanyInfo.setPayJson(json);
                payCompanyInfoMapper.insertSelective(payCompanyInfo);
            }
        }
    }

    @Override
    public List<PayCompanyInfo> selectpayCompanyInfo(String companyId) {
        PayCompanyInfo payCompanyInfo=new PayCompanyInfo();
        List<PayCompanyInfo> list=new ArrayList<>();
        payCompanyInfo.setCompanyId(companyId);
        list= payCompanyInfoMapper.select(payCompanyInfo);
        for (PayCompanyInfo companyInfo : list) {
            String json =companyInfo.getPayJson();
            json = StringEscapeUtils.unescapeJava(json);
            companyInfo.setPayJson(json);
            if (companyInfo.getPayChannel()==1){
                companyInfo.setWxPayJsonVo(JSONObject.parseObject(json,WXPayJsonVo.class));

            }else if (companyInfo.getPayChannel() ==2){
                companyInfo.setZfbPayJsonVo(JSONObject.parseObject(json,ZFBPayJsonVo.class));
            }else if (companyInfo.getPayChannel() ==3){
                companyInfo.setIcbcVo(JSONObject.parseObject(json,ICBCVo.class));
            }else if (companyInfo.getPayChannel() ==4){
                companyInfo.setUnionJsonPayVo(JSONObject.parseObject(json,UnionJsonPayVo.class));
            }else if (companyInfo.getPayChannel() ==5){
                companyInfo.setQingDaoNongShangJsonVo(JSONObject.parseObject(json,QingDaoNongShangJsonVo.class));
            }else if (companyInfo.getPayChannel() ==6){
                companyInfo.setShouQianBaVo(JSONObject.parseObject(json,ShouQianBaVo.class));
            }else if (companyInfo.getPayChannel() ==7){
                companyInfo.setCcbbVo(JSONObject.parseObject(json,CCBBVo.class));
            }else if (companyInfo.getPayChannel() ==8){
                companyInfo.setKancloudVo(JSONObject.parseObject(json,KancloudVo.class));
            }else if (companyInfo.getPayChannel() ==9){
                companyInfo.setLeShua(JSONObject.parseObject(json,LeShua.class));
            }else if (companyInfo.getPayChannel() ==10){
                companyInfo.setChinaums(JSONObject.parseObject(json,Chinaums.class));
            }else if (companyInfo.getPayChannel() ==11){
                companyInfo.setUnionSchool(JSONObject.parseObject(json,UnionSchool.class));
            }else if (companyInfo.getPayChannel() ==12){
                companyInfo.setFuHuiBaVo(JSONObject.parseObject(json,FuHuiBaVo.class));
            }else if (companyInfo.getPayChannel() ==15){
                companyInfo.setAbc(JSONObject.parseObject(json,ABC.class));
            }else if (companyInfo.getPayChannel() ==16){
                companyInfo.setQsf(JSONObject.parseObject(json,QSF.class));
            }else if (companyInfo.getPayChannel() ==19){
                companyInfo.setHuiYinTong(JSONObject.parseObject(json,HuiYinTong.class));
            }else if (companyInfo.getPayChannel() ==20){
                companyInfo.setBocom(JSONObject.parseObject(json,Bocom.class));
            }else if (companyInfo.getPayChannel() ==21){
                companyInfo.setSandHippoVo(JSONObject.parseObject(json,SandHippoVo.class));
            }else if (companyInfo.getPayChannel() ==22){
                companyInfo.setLaKaLaVo(JSONObject.parseObject(json,LaKaLaVo.class));
            }else if (companyInfo.getPayChannel() ==23){
                companyInfo.setPsbc(JSONObject.parseObject(json,PSBC.class));
            }else if (companyInfo.getPayChannel() ==24){
                companyInfo.setSaoBei(JSONObject.parseObject(json,SaoBei.class));
            }else if (companyInfo.getPayChannel() ==25){
                companyInfo.setHaiKeVo(JSONObject.parseObject(json,HaiKeVo.class));
            }else if (companyInfo.getPayChannel() ==26){
                companyInfo.setFuYouVo(JSONObject.parseObject(json,FuYouVo.class));
            }else if (companyInfo.getPayChannel() ==27){
                companyInfo.setLongZhiMengVo(JSONObject.parseObject(json,LongZhiMengVo.class));
            }else if (companyInfo.getPayChannel() ==28){
                companyInfo.setAliSchoolVo(JSONObject.parseObject(json,AliSchoolVo.class));
            }else if (companyInfo.getPayChannel() ==29){
                companyInfo.setZhongShanTongVO(JSONObject.parseObject(json,ZhongShanTongVO.class));
            }else if (companyInfo.getPayChannel() ==30){
                companyInfo.setZhaoHangYiWangTongVO(JSONObject.parseObject(json,ZhaoHangYiWangTongVO.class));
            }else if (companyInfo.getPayChannel() ==31){
                companyInfo.setQuZhouYiMaFuVO(JSONObject.parseObject(json,QuZhouYiMaFuVO.class));
            }else if (companyInfo.getPayChannel() ==32){
                companyInfo.setJianHangShengHuoVO(JSONObject.parseObject(json,JianHangShengHuoVO.class));
            }else if(companyInfo.getPayChannel() == 33){
                companyInfo.setCcbPayVo(JSONObject.parseObject(json, CCBPayVo.class));
            }else if (companyInfo.getPayChannel() ==34){
                companyInfo.setYinshengVo(JSONObject.parseObject(json,YinShengVo.class));
            }else if (companyInfo.getPayChannel() ==37){
                companyInfo.setLanzhouVo(JSONObject.parseObject(json,LanzhouVo.class));
            }else if (companyInfo.getPayChannel() ==38){
                companyInfo.setNonghangjfVo(JSONObject.parseObject(json, NonghangjfVo.class));
            }

        }
        return list;
    }

    /*
    ** @author lu
    * @date 2019/7/5 14:12
    * @param [company]
    * @return void
    * @description 修改
    */
    @Override
    public void updateCompany(Company company) {
        //修改客户
        companyMapper.updateByPrimaryKeySelective(company);
        //获取客户表下的支付公司信息的ID，删除，再新增
        String strId=company.getOldPayChannel();
        String[] strArr = strId.split(",");
        PayCompanyInfo payCompanyInfo1 =new PayCompanyInfo();
        for (int i = 0; i < strArr.length; ++i){
            if (!strArr[i].equals("")){
                PayCompanyInfo payCompanyInfo=payCompanyInfoMapper.selectByPrimaryKey(Integer.valueOf(strArr[i]));
                if (payCompanyInfo.getPayChannel()==1){
                    //微信
                    WXPayJsonVo wxPayJsonVo= JSONObject.parseObject(payCompanyInfo.getPayJson(),WXPayJsonVo.class);
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(wxPayJsonVo.getAppID());
                    wxPayInfo.setMchId(wxPayJsonVo.getMchid());
                    wxPayInfoMapper.delete(wxPayInfo);
                }else if(payCompanyInfo.getPayChannel()==2){
                    //支付宝
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(payCompanyInfo.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }
                payCompanyInfo1.setId(Integer.valueOf(strArr[i]));
                payCompanyInfoMapper.delete(payCompanyInfo1);
            }

        }

        //是否需要配置设备的支付json
        if (!company.getIsEverySet()){
            byte payChannel;
            for (PayCompanyInfo payCompanyInfo : company.getPayCompanyInfos()) {
                String json="";
                if (payCompanyInfo.getWxPayJsonVo()!=null){
                    payChannel=1;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getWxPayJsonVo());
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(payCompanyInfo.getWxPayJsonVo().getAppID());
                    wxPayInfo.setMchId(payCompanyInfo.getWxPayJsonVo().getMchid());
                    wxPayInfo.setWXkey(payCompanyInfo.getWxPayJsonVo().getKey());
                    wxPayInfo.setCreateTime(new Date());

                }else if (payCompanyInfo.getZfbPayJsonVo()!=null){
                    payChannel=2;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getZfbPayJsonVo());
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(payCompanyInfo.getZfbPayJsonVo().getAppID());
                    aliPayInfo.setAliPayPublicKey(payCompanyInfo.getZfbPayJsonVo().getAlipayPublicKey());
                    aliPayInfo.setCreateTime(new Date());
                    aliPayInfoMapper.insert(aliPayInfo);
                }else if (payCompanyInfo.getIcbcVo()!=null){
                    payChannel=3;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getIcbcVo());
                }else if (payCompanyInfo.getUnionJsonPayVo()!=null){
                    payChannel=4;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getUnionJsonPayVo());
                }else if (payCompanyInfo.getQingDaoNongShangJsonVo()!=null){
                    payChannel=5;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getQingDaoNongShangJsonVo());
                }else if (payCompanyInfo.getShouQianBaVo()!=null){
                    payChannel=6;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getShouQianBaVo());
                }else if (payCompanyInfo.getCcbbVo()!=null){
                    payChannel=7;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getCcbbVo());
                }else if (payCompanyInfo.getKancloudVo()!=null){
                    payChannel=8;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getKancloudVo());
                }else if (payCompanyInfo.getLeShua()!=null){
                    payChannel=9;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getLeShua());
                }else if (payCompanyInfo.getChinaums()!=null){
                    payChannel=10;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getChinaums());
                }else if (payCompanyInfo.getUnionSchool()!=null){
                    payChannel=11;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getUnionSchool());
                }else if (payCompanyInfo.getFuHuiBaVo()!=null){
                    payChannel=12;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getFuHuiBaVo());
                }else if (payCompanyInfo.getAbc()!=null){
                    payChannel=15;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getAbc());
                }else if (payCompanyInfo.getQsf()!=null){
                    payChannel=16;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getQsf());
                }else if (payCompanyInfo.getHuiYinTong()!=null){
                    payChannel=19;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getHuiYinTong());
                }else if (payCompanyInfo.getBocom()!=null){
                    payChannel=20;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getBocom());
                }else if (payCompanyInfo.getLaKaLaVo()!=null){
                    payChannel=22;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getLaKaLaVo());
                }else if (payCompanyInfo.getPsbc()!=null){
                    payChannel=23;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getPsbc());
                }else if (payCompanyInfo.getSaoBei()!=null){
                    payChannel=24;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getSaoBei());
                }else if (payCompanyInfo.getHaiKeVo()!=null){
                    payChannel=25;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getHaiKeVo());
                }else if (payCompanyInfo.getFuYouVo()!=null){
                    payChannel=26;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getFuYouVo());
                }else if (payCompanyInfo.getLongZhiMengVo()!=null){
                    payChannel=27;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getLongZhiMengVo());
                }else if (payCompanyInfo.getAliSchoolVo()!=null){
                    payChannel=28;
                    payCompanyInfo.setPayChannel(payChannel);

                    json = JSON.toJSONString(payCompanyInfo.getAliSchoolVo());
                }else if (payCompanyInfo.getSuiXingPay()!=null){
                    payChannel=14;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getSuiXingPay());
                }else if (payCompanyInfo.getZhongShanTongVO()!=null){
                    payChannel=29;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getZhongShanTongVO());
                }else if (payCompanyInfo.getZhaoHangYiWangTongVO()!=null){
                    payChannel=30;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getZhaoHangYiWangTongVO());
                }else if (payCompanyInfo.getQuZhouYiMaFuVO()!=null){
                    payChannel=31;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getQuZhouYiMaFuVO());
                }else if (payCompanyInfo.getJianHangShengHuoVO()!=null){
                    payChannel=32;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getJianHangShengHuoVO());
                }else if(payCompanyInfo.getCcbPayVo()!=null){
                    payChannel=33;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getCcbPayVo());
                }else if(payCompanyInfo.getYinshengVo()!=null){
                    payChannel=34;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getYinshengVo());
                }else if(payCompanyInfo.getLanzhouVo()!=null){
                    payChannel=37;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getLanzhouVo());
                }else if(payCompanyInfo.getNonghangjfVo()!=null){
                    payChannel=38;
                    payCompanyInfo.setPayChannel(payChannel);
                    json = JSON.toJSONString(payCompanyInfo.getNonghangjfVo());
                }
                payCompanyInfo.setCompanyId(company.getCompanyId());

                payCompanyInfo.setPayJson(json);
                payCompanyInfoMapper.insertSelective(payCompanyInfo);
            }
        }
    }

    /*
    ** @author lu
    * @date 2019/7/5 14:11
    * @param [ids]
    * @return void
    * @description 删除客户
    */
    @Override
    public void deleteBatchByIds(Object[] ids) {
        PayCompanyInfo payCompanyInfo =new PayCompanyInfo();

        for (int i = 0; i < ids.length; i++) {
            Company company= companyMapper.selectByPrimaryKey(ids[i]);
            payCompanyInfo.setCompanyId(company.getCompanyId());

            //查询出配置文件,再删除
            List<PayCompanyInfo> list= payCompanyInfoMapper.select(payCompanyInfo);
            for (PayCompanyInfo companyInfo : list) {
                if (companyInfo.getPayChannel()==1){
                    //微信
                    WXPayJsonVo wxPayJsonVo= JSONObject.parseObject(companyInfo.getPayJson(),WXPayJsonVo.class);
                    WXPayInfo wxPayInfo=new WXPayInfo();
                    wxPayInfo.setAppid(wxPayJsonVo.getAppID());
                    wxPayInfo.setMchId(wxPayJsonVo.getMchid());
                    wxPayInfoMapper.delete(wxPayInfo);
                }else if(companyInfo.getPayChannel()==2){
                    //支付宝
                    ZFBPayJsonVo zfbPayJsonVo=JSONObject.parseObject(companyInfo.getPayJson(),ZFBPayJsonVo.class);
                    AliPayInfo aliPayInfo=new AliPayInfo();
                    aliPayInfo.setAliAppid(zfbPayJsonVo.getAppID());
                    aliPayInfo.setAliPayPublicKey(zfbPayJsonVo.getAlipayPublicKey());
                    aliPayInfoMapper.delete(aliPayInfo);
                }
            }
            payCompanyInfoMapper.delete(payCompanyInfo);
            companyMapper.deleteByPrimaryKey(ids[i]);
        }
    }

    @Override
    public Result createUnfiedpayCompany(Company company) {
        company.setPayChannel("000000000000000000000000000000");
        // 加密密码
        passwordHelper.companyPassword(company);
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            company.setCreateTime(dd.parse(dd.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取今天的时间对比时间今天生成的单号，获取单号的后三位
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String date=df.format(new Date());
        String strMAXcompanyId= companyMapper.selectMAXcompanyId(date);
        if (StringUtils.isEmpty(strMAXcompanyId)){
            company.setCompanyId(date+"001");
        }else {
            Integer intMAXcompanyId =Integer.valueOf(strMAXcompanyId);
            if (intMAXcompanyId<9){
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+"00"+intMAXcompanyId);
            }else if (9<=intMAXcompanyId && intMAXcompanyId < 99){
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+"0"+intMAXcompanyId);
            }else {
                intMAXcompanyId=intMAXcompanyId+1;
                company.setCompanyId(date+intMAXcompanyId);
            }
        }
        company.setCompanyId(company.getCompanyId() + platform);
        int i=companyMapper.insertSelective(company);
        Result result=new Result();
        if (i>0){
            Map<String,String> map=new HashMap<>();
            map.put("companyId",company.getCompanyId());
            result.setCode("0000");
            result.setMsg("成功");
            result.setData(map);
            return result;
        }else {
            result.setCode("0001");
            result.setMsg("新增失败");
            return result;
        }
    }

    @Override
    public Result selectCompany(String companyId) {
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("companyId",companyId);
        Result result=new Result();
        try {
            Company company=mapper.selectOneByExample(example);
            if (company==null){
                result.setCode("0003");
                result.setMsg("查询商户不存在");
                return result;
            }
            result.setCode("0000");
            result.setMsg("成功");
            result.setSuccess(true);
            result.setData(company);
            return result;
        }catch (Exception e){
            result.setCode("2301");
            result.setMsg("查询商户信息异常");
            return result;
        }
    }

    @Override
    public Result updateCompanyPay(Company company) {
        Result result=new Result();
        if (company.getCompanyId()==null || company.getCompanyId().equals("")){
            result.setCode("0008");
            result.setMsg("修改单位的单位编号为空");
            return result;
        }
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",company.getCompanyId());
        int i=mapper.updateByExampleSelective(company,example);

        if (i<=0){
            result.setCode("0001");
            result.setMsg("修改失败");
            return result;
        }
        result.setCode("0000");
        result.setSuccess(true);
        result.setMsg("成功");
        return result;
    }

    /*
     * @Description:删除单位信息
     * @Author: lu
     * @Date: 2022/4/1 9:40
     * @param companyId:
     * @return: tech.wetech.admin.core.utils.Result
     **/
    @Override
    public Result deletes(String companyId) {
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        Result result=new Result();
        if (companyId==null || companyId.equals("")){
            result.setCode("0008");
            result.setMsg("删除单位的单位编号为空");
            return result;
        }
        criteria.andEqualTo("companyId",companyId);
        int i=mapper.deleteByExample(example);

        if (i<=0){
            result.setCode("0001");
            result.setMsg("删除单位信息失败");
            return result;
        }
        result.setCode("0000");
        result.setMsg("删除成功");
        result.setSuccess(true);
        return result;
    }

    @Override
    public List<Company> querySelectList(Company company, PageQuery pageQuery) {
        if (pageQuery.getOrderBy() != null) {
            pageQuery.setOrderBy(StringUtil.convertByStyle(pageQuery.getOrderBy(), Style.camelhump));
        }
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("id DESC");
        if (company.getUserName()!=null || company.getCompanyName()!=null){
            //判断商户订单号是否为空，不为空进行模糊查询
            if (company.getUserName()!=null){


                String userName= "%"+company.getUserName()+"%";
                // 创建条件对象
                criteria.andLike("userName",userName);

            }
            //判断平台订单号是否为空，不为空进行模糊查询
            if (company.getCompanyName()!=null){


                String companyName= "%"+company.getCompanyName()+"%";
                // 创建条件对象
                criteria.andLike("companyName",companyName);

            }
        }
        return PageHelper.startPage(pageQuery)
                .doSelectPage(() -> mapper.selectByExample(example));
    }
}
